package lecture;


import menus.LectureMenuItem;
import menus.MenuCategory;
import beans.SessionManagedBean;
import com.icesoft.faces.component.menubar.MenuItem;
import java.util.ArrayList;
import java.util.List;
import javax.el.MethodExpression;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.event.MethodExpressionActionListener;
import user.Role;
import user.User;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="lectureBean")
@CustomScoped(value="#{window}")
public class LectureManagedBean {
        
    /**
     * choosen lecture
     */
    private Lecture lecture;
    /**
     * list of menu elements of the upper menu of the lecture
     */
    private List<MenuItem> firstLevelMenu;
    /**
     * list of menu elements of the lower menu of the lecture
     */
    private List<MenuItem> secondLevelMenu;
    /**
     * last choosen category
     */
    private MenuCategory selectedCategory;
    
    private LectureMenuItem selectedSubCategory;
    /**
     * 
     */
    public LectureManagedBean() {
        this.firstLevelMenu = new ArrayList<MenuItem>();
        this.secondLevelMenu = new ArrayList<MenuItem>();
    }
        
    /**
     * set up the upper menu
     */
    public void initFirstLevelMenu() {
        Role role = this.getUserRole();
        List<MenuCategory> categories = this.lecture.getCategorys();        
        MenuItem item;
        this.firstLevelMenu.clear();
        for (final MenuCategory c: categories) {            
            if (c.getRights().hasViewRight(role)) {
                    item = new MenuItem();
                    item.setValue(c.getName());                    
                    String link="lecture.jsf";
                    if (c.getMenuItems().size() > 0)
                        link = c.getMenuItems().get(0).getLink();
                    // item.setId(c.getName().replace(" ", "___"));
                    item.setActionExpression(getActionExp(link)); 
                    final LectureManagedBean l = this;
                    item.addActionListener(new ActionListener() {
                        
                        @Override
                        public void processAction(ActionEvent ae) throws AbortProcessingException {
                            l.selectedCategory = c;
                            l.initSecondLevelMenu();
                            if (l.getSecondLevelMenu().size() > 0)
                                l.setSelectedSubCategory(c.getMenuItems().get(0));
                        }
                        
                    });
                    //item.addActionListener(getActionListenerExp("#{lectureBean.categorySelected}"));
                    this.firstLevelMenu.add(item);          
            }            
        }
    }
    
    /**
     * set up the lower menu
     */
    public void initSecondLevelMenu() {
        Role role = this.getUserRole();
        this.secondLevelMenu.clear();
        MenuItem item;
        System.out.println("Menü: " + this.selectedCategory.getName() + " Größe: " + this.selectedCategory.getMenuItems().size());
        for (final LectureMenuItem l: this.selectedCategory.getMenuItems()) {
            if (l.getRights().hasViewRight(role)) {
                item = new MenuItem();
                item.setValue(l.getName());
                item.setActionExpression(getActionExp(l.getLink()));
                final LectureManagedBean t = this;
                item.addActionListener(new ActionListener() {
                     @Override
                     public void processAction(ActionEvent ae) throws AbortProcessingException {
                         t.selectedSubCategory = l;
                     }
                });
                this.secondLevelMenu.add(item);
            }
        }
    }
    
    public List<MenuItem> getFirstLevelMenu() {
        return this.firstLevelMenu;
    }
    
    public List<MenuItem> getSecondLevelMenu() {
        return this.secondLevelMenu;
    }

    public Lecture getLecture() {
        return lecture;
    }

    /**
     * set a new lecture.
     * this methods set everythin up when a different lecture was selected
     * @param lecture the new lecture
     */
    public void setLecture(Lecture lecture) {
        this.lecture = lecture;
        this.initFirstLevelMenu();
        this.selectedCategory = this.lecture.getCategorys().get(0);
        this.selectedSubCategory = this.selectedCategory.getMenuItems().get(0);
        System.out.println("Vorlesung gesetzt: " + this.selectedCategory.getName());
        this.initSecondLevelMenu();
    }
   
     // Method to use when you want to get an Action method
     private MethodExpression getActionExp(String actionName){
         FacesContext context = FacesContext.getCurrentInstance();
         return context.getApplication().getExpressionFactory()
                 .createMethodExpression(context.getELContext(), actionName, String.class, new Class[0]);
     }
     
         
     // Method to use when you want to get an ActionListener method
     public MethodExpressionActionListener getActionListenerExp(String actionListenerName){
         FacesContext context = FacesContext.getCurrentInstance();
         MethodExpression mExp = context.getApplication().getExpressionFactory()
                 .createMethodExpression(context.getELContext(), actionListenerName, null,
                 new Class[] {ActionEvent.class});
         return new MethodExpressionActionListener(mExp);
     }
    
    public MenuCategory getSelectedCategory() {
        return selectedCategory;
    }

    public LectureMenuItem getSelectedSubCategory() {
        return selectedSubCategory;
    }
    
    public boolean hasEditRights() {
        Role role = this.getUserRole();
        return this.selectedSubCategory.getRights().hasEditRight(role);
    }
    
    public boolean hasViewRights() {
        Role role = this.getUserRole();
        return this.selectedSubCategory.getRights().hasViewRight(role);
    }
    
    private Role getUserRole() {
        FacesContext context = FacesContext.getCurrentInstance();        
        SessionManagedBean sb = (SessionManagedBean) context.getApplication().evaluateExpressionGet(context, "#{sessionBean}", SessionManagedBean.class);
        User user = sb.getUser();
        return user.getUserRole(this.lecture.getInfo().getName());
    }

    public void setSelectedSubCategory(LectureMenuItem selectedSubCategory) {
        this.selectedSubCategory = selectedSubCategory;
    }
    
}